/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.shop.shangcheng.banner;
import com.google.common.collect.Maps;

import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.modules.shop.entity.address.Address;
import com.thinkgem.jeesite.modules.shop.entity.banner.Banner;
import com.thinkgem.jeesite.modules.shop.entity.cate_gory.Cate_Gory;
import com.thinkgem.jeesite.modules.shop.entity.goods.Goods;
import com.thinkgem.jeesite.modules.shop.entity.order.Order;
import com.thinkgem.jeesite.modules.shop.entity.save.Save;
import com.thinkgem.jeesite.modules.shop.entity.shop_car.ShopCar;
import com.thinkgem.jeesite.modules.shop.service.address.AddressService;
import com.thinkgem.jeesite.modules.shop.service.banner.BannerService;
import com.thinkgem.jeesite.modules.shop.service.cate_gory.Cate_GoryService;
import com.thinkgem.jeesite.modules.shop.service.goods.GoodsService;
import com.thinkgem.jeesite.modules.shop.service.order.OrderService;
import com.thinkgem.jeesite.modules.shop.service.save.SaveService;
import com.thinkgem.jeesite.modules.shop.service.shop_car.ShopCarService;
import com.thinkgem.jeesite.modules.shop.web.banner.BannerController;
import com.thinkgem.jeesite.modules.sys.entity.User;
import com.thinkgem.jeesite.modules.sys.service.SystemService;
import com.thinkgem.jeesite.modules.sys.utils.UserUtils;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.thinkgem.jeesite.modules.sys.entity.Office;
import com.thinkgem.jeesite.modules.sys.entity.Role;
import com.google.common.collect.Lists;
import com.thinkgem.jeesite.modules.sys.security.SystemAuthorizingRealm;
import org.apache.shiro.web.util.WebUtils;
import com.thinkgem.jeesite.modules.sys.security.FormAuthenticationFilter;
import com.thinkgem.jeesite.modules.sys.security.UsernamePasswordToken;
import org.apache.shiro.SecurityUtils;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

/**
 * 首页
 * @author sodo
 * @version 2019-05-15
 */
@Controller
@RequestMapping(value = "shop")
public class shopcontroller extends BaseController {

	@Autowired
	private OrderService orderService;
	@Autowired
	private AddressService addressService;
@Autowired
private  ShopCarService shopCarService;
	@Autowired
	private BannerService bannerService;
	@Autowired
	private Cate_GoryService cate_goryService;
	@Autowired
	private SystemService systemService;
    @Autowired
    private SaveService saveService;
	@Autowired
	private GoodsService goodsService;

    /*首页*/
	@RequestMapping("indexList")
	public String indexList(HttpServletRequest request, HttpServletResponse response, Model model) {
		Map<String,Object> map = new HashMap<String, Object>();
		BannerController bc=new BannerController();
		Banner b=new Banner();
		b.setBannertype("1");
		List<Banner> list1= bannerService.findList(b);
		map.put("顶部轮播",list1);

		Cate_Gory c=new Cate_Gory();
		c.setParent_id("一级菜单");
		List<Cate_Gory> list2=cate_goryService.findList(c);
		map.put("一级菜单",list2);

		Cate_Gory cc=new Cate_Gory();
		cc.setCategorytype("1");
		List<Cate_Gory> list3=cate_goryService.findList(cc);
		map.put("生活超市",list3);

		cc.setCategorytype("2");
		List<Cate_Gory> list5=cate_goryService.findList(cc);
		map.put("团购套餐",list5);

		Goods good=new Goods();
		List<Goods> menupage = goodsService.findList(good);
		request.setAttribute("menupage",menupage);

		model.addAttribute("map", map);
		return "modules/modshop/home/index";
	}

	/*兼职*/
	@RequestMapping("video")
	public String video(HttpServletRequest request, HttpServletResponse response, Model model) {
		return "modules/modshop/video/video";
	}
	@RequestMapping("timeline-publish")
	public String timelinePublish(HttpServletRequest request, HttpServletResponse response, Model model) {
		return "modules/modshop/video/timeline-publish";
	}
	@RequestMapping("timeline")
	public String timeline(HttpServletRequest request, HttpServletResponse response, Model model) {
		return "modules/modshop/video/timeline";
	}
	@RequestMapping("jobfrom")
	public String jobfrom(HttpServletRequest request, HttpServletResponse response, Model model) {
		return "modules/modshop/video/jobfrom";
	}
	@RequestMapping("jobdetails")
	public String jobdetails(HttpServletRequest request, HttpServletResponse response, Model model) {
		return "modules/modshop/video/jobdetails";
	}
	/*我的*/
	@RequestMapping("personal")
	public String personal(HttpServletRequest request, HttpServletResponse response, Model model, User user) {
		if (UserUtils.getPrincipal()==null){
			return "modules/modshop/personal/login";
		}
		return "modules/modshop/personal/personal";

	}
	/*跳转登录*/
	@RequestMapping("loginList")
	public String loginList(HttpServletRequest request, HttpServletResponse response, Model model, User user) {
		return "modules/modshop/personal/login";
	}
	/*登录*/
	@RequestMapping("login")
	@ResponseBody
	public Map<String,Object> login(HttpServletRequest request, HttpServletResponse response, Model model) {
		Map<String,Object> map = Maps.newHashMap();
		SystemAuthorizingRealm.Principal principal = UserUtils.getPrincipal();
		// 如果已经登录，则跳转到首页
		if(principal != null){
			map.put("status","2");
			map.put("message","已登陆，请勿重复登陆");
			return map;
		}
		String username = WebUtils.getCleanParam(request, FormAuthenticationFilter.DEFAULT_USERNAME_PARAM);
		boolean rememberMe = WebUtils.isTrue(request, FormAuthenticationFilter.DEFAULT_REMEMBER_ME_PARAM);
		boolean mobile = WebUtils.isTrue(request, FormAuthenticationFilter.DEFAULT_MOBILE_PARAM);
		String pass = request.getParameter("password");
		String host = StringUtils.getRemoteAddr(request);
		try {
			UsernamePasswordToken usernamePasswordToken = new UsernamePasswordToken(username,
					pass.toCharArray(),rememberMe,host,null,mobile);
			SecurityUtils.getSubject().login(usernamePasswordToken);
		}catch (Exception e){
			map.put("status","1");
			map.put("message","账号或密码错误，登录失败");
			return map;
		}
		User user = UserUtils.getUser();
		if(user != null && StringUtils.isNotEmpty(user.getId())){
			map.put("status","0");
			map.put("message","登录成功");
		}
		return map;
	}
	//注销退出当前用户and切换用户
	@RequestMapping("logout")
	public String logout() {
		UserUtils.getSubject().logout();
		return "modules/modshop/personal/login";
	}
	//注销退出当前用户
	@RequestMapping("logoutno")
	public String logoutno() {
		UserUtils.getSubject().logout();
		return "modules/modshop/personal/personal";
	}


	@RequestMapping("easyLife")/*立即结账*/
	public String shop2(HttpServletRequest request, HttpServletResponse response, Model model) {
		return "modules/modshop/easyLife/easyLife";
	}

	@RequestMapping("address")/*收货地址*/
	public String shop4(HttpServletRequest request, HttpServletResponse response, Model model) {
		return "modules/modshop/easyLife/order-address";
	}
	@RequestMapping("create")/*订单结算*/
	public String shop5(HttpServletRequest request, HttpServletResponse response, Model model) {
		return "modules/modshop/easyLife/order-create";
	}
	@RequestMapping("result")/*支付成功*/
	public String shop6(HttpServletRequest request, HttpServletResponse response, Model model) {
		return "modules/modshop/easyLife/order-result";
	}


	/*收藏*/
	@RequestMapping("saveList")
	public String saveList(HttpServletRequest request, HttpServletResponse response, Model model) {
		if(UserUtils.getPrincipal() == null){
			return "modules/modshop/personal/login";
		}
		Save save=new Save();
		//获得当前用户
		User user = UserUtils.getUser();
		save.setUser(user);
		List<Save> list = saveService.findList(save);
		model.addAttribute("list", list);

		return "modules/modshop/personal/collections";

	}
	/*收藏添加*/
	@RequestMapping("saveAdd")
	@ResponseBody
	public String save(Save save, Goods goods,String id, HttpServletRequest request, HttpServletResponse response) {
		if(UserUtils.getPrincipal() == null){
			return "s";
		}
		User user = UserUtils.getUser();
		save.setUser(user);
		goods=goodsService.get(id);
		save.setGoods(goods);
		//save.setGoods(goodsService.get(request.getParameter("id")));
		System.out.println(save);
		saveService.insert(save);
		return "c";

	}


	/*删除收藏*/
	@RequestMapping("saveDelete")
	public String saveDelete(HttpServletRequest request, HttpServletResponse response, Model model) {
		Save save=new Save();
		saveService.delete(save);
		System.out.println("删除");
		List<Save> list = saveService.findList(save);
		model.addAttribute("list", list);
		return "modules/modshop/personal/collections";
	}
	/*跳转注册*/
	@RequestMapping("registerList")
	public String registerList(HttpServletRequest request, HttpServletResponse response, Model model) {
		return "modules/modshop/personal/register";
	}

	/*注册*/
	@RequestMapping("registeruser")
	@ResponseBody
	public Map<String,Object> registeruser(User sysUser, HttpServletRequest request, HttpServletResponse response, Model model) {
		Map<String,Object> map = Maps.newHashMap();
		String pass = request.getParameter("password");
		String username = request.getParameter("username");
		String name = request.getParameter("name");
		sysUser.setLoginName(username);
		//判断注册账号是否存在
		List<User> users= systemService.findUser(sysUser);
		if (!(users.isEmpty())){
			map.put("status","0");
			map.put("message","账号已存在");
			return map;
		}
		sysUser.setName(name);
		sysUser.setPassword(SystemService.entryptPassword(pass));

		List<Role> roleList = Lists.newArrayList();
		roleList.add(new Role("6"));
		sysUser.setRoleList(roleList);
		sysUser.setCompany(new Office("1"));
		sysUser.setOffice(new Office("10"));
		sysUser.setCreateBy(sysUser);
		sysUser.setUpdateBy(sysUser);
		systemService.saveUser(sysUser);
		// 清除当前用户缓存
		if (sysUser.getLoginName().equals(UserUtils.getUser().getLoginName())){
			UserUtils.clearCache();
			//UserUtils.getCacheMap().clear();
		}
		map.put("status","1");
		map.put("message","注册成功");
		return map;
	}
	@RequestMapping("mistiming")/*商品*/
	public String shop1(HttpServletRequest request) {
		Goods good=new Goods();
		List<Goods> menupage = goodsService.findList(good);
		request.setAttribute("menupage",menupage);
		System.out.println(menupage);

		return "modules/modshop/mistiming/mistiming";
	}

	@RequestMapping("easyLifedetails")/*按商品名字查找*/
	public String easyLifedetails(String name,HttpServletRequest request) {

		Goods goods = new Goods();
		goods.setName(name);
		List<Goods> easyLifedetails = goodsService.findList(goods);
		request.setAttribute("easyLifedetails",easyLifedetails);

		System.out.println(easyLifedetails);

		return "modules/modshop/easyLife/easyLifedetails";
	}


	@RequestMapping("AddressindexList1")
	public String shopAddress1(Address address,HttpServletRequest request, HttpServletResponse response, Model model) {

		Page<Address> page = addressService.findPage(new Page<Address>(request,response),address);
		model.addAttribute("page",page);
		return "modules/modshop/personal/customer";
	}
	@RequestMapping("indexListUpdate")
	public String shopAddress2(Address address,HttpServletRequest request, HttpServletResponse response, Model model) {
		String addressId = request.getParameter("addressId");
		address.setId(addressId);
		address = addressService.get(address);
		model.addAttribute("address",address);
		model.addAttribute("addressID",address.getId());
		return "modules/modshop/personal/Addressfrom";
	}
	@RequestMapping("indexListSave")
	public String shopAddress3(User user,Address address,HttpServletRequest request, HttpServletResponse response, Model model) {
		user = systemService.getUser("51d105bf16214e9ea8e180419694694b");
		address.setUser(user);
		address.setDefaul("1");
		addressService.save(address);
		return  "redirect:AddressindexList1";
	}
	@RequestMapping("indexListAdd")
	public String shopAddress4(User user,HttpServletRequest request, HttpServletResponse response, Model model) {
		user = systemService.getUser("51d105bf16214e9ea8e180419694694b");
		model.addAttribute("user",user);
		return "modules/modshop/personal/Addressfrom";
	}
	@RequestMapping("indexListDelete")
	public String shopDelete(Address address,HttpServletRequest request, HttpServletResponse response, Model model) {
		String addressId = request.getParameter("addressId");
		address.setId(addressId);
		addressService.delete(address);
		return "redirect:AddressindexList1";
	}

	@RequestMapping("indexListDef")
	public String shopdefault(User user,Address address,HttpServletRequest request, HttpServletResponse response, Model model) {
		user = systemService.getUser("51d105bf16214e9ea8e180419694694b");
		address = addressService.get(address.getId());
		address.setUser(user);
		address.setDefaul("1");
		addressService.updateDefaultByUserId(address);
		address.setDefaul("0");
		addressService.save(address);
		return  "redirect:AddressindexList1";
	}



	@RequestMapping("AddressindexList2")
	public String shopOrder1(User user,Order order, HttpServletRequest request, HttpServletResponse response, Model model) {
		user = systemService.getUser("51d105bf16214e9ea8e180419694694b");
		order.setUser(user);
		Address address = new Address();
		address.setDefaul("0");
		List<Address> list = addressService.findList(address);
		Address address1 = list.get(0);
		model.addAttribute("address",address1);
		List<Order> list1 = orderService.findList(order);
		int sum=0;
		for (Order o:list1) {
			sum+=Integer.parseInt(o.getGoods().getPrice());
		}
		model.addAttribute("sum",sum);
		model.addAttribute("list",list1);
		return "modules/modshop/easyLife/order-create";
	}
	@RequestMapping("indexList2OrderPay")
	public String shopOrder2(HttpServletRequest request, HttpServletResponse response, Model model) {
		int sum=Integer.parseInt(request.getParameter("num"));
		model.addAttribute("sum",sum);
		return "modules/modshop/easyLife/order-result";
	}
	@RequestMapping("indexListAddre")
	public String shopOrder3(User user,Address address,HttpServletRequest request, HttpServletResponse response, Model model) {
		user = systemService.getUser("51d105bf16214e9ea8e180419694694b");
		address.setUser(user);
		List<Address> list = addressService.findList(address);
		model.addAttribute("address",list);
		return "modules/modshop/easyLife/order-address";
	}
	@RequestMapping("indexListDfalt")
	public String shopOrder4(User user,Address address,HttpServletRequest request, HttpServletResponse response, Model model) {

		user = systemService.getUser("51d105bf16214e9ea8e180419694694b");
		address = addressService.get(address.getId());
		address.setUser(user);
		address.setDefaul("1");
		addressService.updateDefaultByUserId(address);
		address.setDefaul("0");
		addressService.save(address);
		return "redirect:AddressindexList2";
	}
	@RequestMapping("indexListDou")
	public String shopOrder5(User user,Order order,HttpServletRequest request, HttpServletResponse response, Model model) {
		user = systemService.getUser("51d105bf16214e9ea8e180419694694b");
		order.setUser(user);
		Address address = new Address();
		address.setDefaul("0");
		List<Address> list = addressService.findList(address);
		Address address1 = list.get(0);
		model.addAttribute("address",address1);
		List<Order> list1 = orderService.findList(order);
		model.addAttribute("list",list1);
		return "modules/modshop/easyLife/order-create2";
	}

	@RequestMapping("cart")/*购物车*/
	public String shop1(ShopCar shopCar, HttpServletRequest request, HttpServletResponse response, Model model) {
		//得到当前登录用户
		User user= UserUtils.getUser();
		shopCar.setUser(user);

		//删除功能
		String delete_id = request.getParameter("id");
		System.out.println(delete_id);
		ShopCar shopCar1 = shopCarService.get(delete_id);
		shopCarService.delete(shopCar1);

		List<ShopCar> list= shopCarService.findList(shopCar);
		model.addAttribute("list",list);
		return "modules/modshop/easyLife/cart";
	}
	@RequestMapping("add_car")/*添加购物车*/
	public String add_car(ShopCar shopCar,HttpServletRequest request, HttpServletResponse response, Model model) {
		shopCar.setGoods(goodsService.get(request.getParameter("")));
		shopCarService.save(shopCar);
		return "";
	}
	//购物车结算
	@RequestMapping("jiesuan")
	public String jiesuan(Address address,User user,HttpServletRequest request, HttpServletResponse response, Model model,String mapList) {

		Order order = new Order();

		User user1= UserUtils.getUser();//得到当前用户
		address.setUser(user);
		address.setDefaul("0");

		List<Address> list = addressService.findList(address);
		Address address1 = list.get(0);
		model.addAttribute("address",address1);


		JSONArray fromObject = JSONArray.fromObject(mapList);

		JSONArray jsonArray = fromObject;//将String转化为JSONArray对象
		for (int i = 0; i < jsonArray.size(); i++) {
			JSONObject jsonObject = jsonArray.getJSONObject(i);//获取循环体中的每一个JSONObject对象
			String goodsId = jsonObject.optString("goodsId");
			String goodsNum = jsonObject.optString("goodsNum");
			String shopcarId=jsonObject.optString("shopcarId");
			shopCarService.delete(shopCarService.get(shopcarId));

			order.setUser(user1);
			order.setGoods(goodsService.get(goodsId));
			order.setAddress(address1);
			orderService.save(order);

		}
		List<Order> list1 = orderService.findList(new Order());
		model.addAttribute("list",list1);


		return "modules/modshop/easyLife/order-create2";
	}

}